package dhy.com.clawer;


import org.apache.http.Header;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.springframework.http.MediaType;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.List;
import java.util.UUID;

public class PicturePilepine implements Pipeline {

    private final String filename;

    public PicturePilepine(String filename)
    {
    this.filename=filename;
    }

    @Override
    public void process(ResultItems resultItems, Task task) {
        List<String> urls = (List<String>)resultItems.get("url");
        for (String url : urls)
        {
            getImage(url);
        }
    }

    //获取图片
    public String getImage(String url) {
        // 获取HttpClient对象
        CloseableHttpClient httpClient = HttpClients.custom().build();

        // 声明httpGet请求对象
        HttpGet httpGet = new HttpGet(url);

        CloseableHttpResponse response = null;

        OutputStream outstream=null;
        try {
            // 使用HttpClient发起请求，返回response
            response = httpClient.execute(httpGet);
            // 解析response下载图片
            if (response.getStatusLine().getStatusCode() == 200&&response.getEntity()!=null)
            {

                Header contentType = response.getEntity().getContentType();
                String name = contentType.getName();//key: contentType
                String value = contentType.getValue();//value: MediaType类型
                //如果不是图片
                if(!value.equals(MediaType.IMAGE_JPEG_VALUE)&&!value.equals(MediaType.IMAGE_PNG_VALUE))
                {
                    return null;
                }
                // 使用uuid生成图片名
                String imageName="";
                if(value.equals(MediaType.IMAGE_JPEG_VALUE))
                 imageName= UUID.randomUUID().toString() + ".jpg";
                else if(value.equals(MediaType.IMAGE_PNG_VALUE))
                    imageName= UUID.randomUUID().toString() + ".png";
                // 声明输出的文件
                File file = new File(filename);
                if(!file.exists())
                    file.mkdirs();
                file=new File(filename+imageName);
                outstream = new FileOutputStream(file);
                // 使用响应体输出文件
                response.getEntity().writeTo(outstream);

                // 返回生成的图片名
                return imageName;
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if(outstream!=null)
                    outstream.close();
                if (response != null) {
                    // 关闭连接
                    response.close();
                }
                httpClient.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return null;
    }
}
